package clases;

import clases.*;
import java.sql.*;
import java.util.ArrayList;

public class Coneccion {

    private String nom_db = "proyecto";
    private String nom_usu = "root";
    private String password = "jhonaA01";
    private String url = "jdbc:mysql://localhost:3306/" + nom_db;
    public ResultSet rs;
    public Statement st;
    public Connection conn = null;
    public String result = "";


    public String connection(String query, int valor) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery(query);
                rs.next();
                result = rs.getString(valor);
                conn.close();
            }
        } catch (Exception e) {
        }
        return result;
    }

    public void cargar() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery("select * from usuario");
                rs.next();


                conn.close();
            }
        } catch (Exception e) {
        }
    }

    public void insertar(String query) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                st.executeUpdate(query);
                conn.close();
            }
        } catch (Exception e) {
        }
    }

// ADMINISTRADOR
                        // MOSTRAR USUARIOS REGISTRADOS//
    public String fila = "";

    public String mostrarUsuario() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery("select nombre from usuario;");
                rs.next();

                for (int x = 0; x < rs.getRow(); x++) {
                    for (int p=1; p<10; p++) {
                        fila = fila + "<td>" + rs.getString(p) + "</td>";
                    }
                    fila = fila + "</tr>";
                    rs.next();
                }
                conn.close();
            }
        } catch (Exception e) {
        }
        return fila;
    }

    
    
    
    
    
//  CLIENTE
                    //Visualizacion Articulos
    public String psicologia = "";

    public String mostrarArticuloPsicologia() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery("select * from usuario");
                rs.next();

                for (int x = 0; x < rs.getRow(); x++) {
                    psicologia = psicologia + "<tr>";
                    for (int p=2; p<8; p++) {
                        psicologia = psicologia + "<td>" + rs.getString(p) + "</td>";
                    }
                    psicologia = psicologia + "</tr>";

                }
                conn.close();
            }
        } catch (Exception e) {
        }
        return psicologia;
    }

    public String bioquimica = "";

    public String mostrarArticuloBioquimica() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery("select * from usuario");
                rs.next();

                for (int x = 0; x < rs.getRow(); x++) {
                    bioquimica = bioquimica + "<tr>";
                    for (int p=2; p<8; p++) {
                        bioquimica = bioquimica + "<td>" + rs.getString(p) + "</td>";
                    }
                    bioquimica = bioquimica + "</tr>";

                }
                conn.close();
            }
        } catch (Exception e) {
        }
        return bioquimica;
    }


// EDITOR
                    //Lista Articulos a Recomendar

    public String articulo1 = "";

    public String mostrarArticulos1() {

        int tamaño = 0;
        tamaño = tamaño+1;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery("select * from articulopsicologia");
                rs.next();

                for (int x = 0; x < rs.getRow(); x++) {
                    articulo1 = articulo1 + "<tr>";
                    for (int p=2; p<5; p++) {
                        articulo1 = articulo1 + "<td>" + rs.getString(p) + "</td>";
                    }
                    for (int p=7; p<8; p++) {
                        articulo1 = articulo1 + "<td>" + rs.getString(p) + "</td>";
                    }
                    articulo1 = articulo1 + "</tr>";                    

                }
                conn.close();
            }
        } catch (Exception e) {
        }
        return articulo1;
    }


    public String articulo2 = "";

    public String mostrarArticulos2() {

        int tamaño = 0;
        tamaño = tamaño+1; 

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, nom_usu, password);

            if (conn != null) {
                st = conn.createStatement();
                rs = st.executeQuery("select * from articulobioquimica");
                rs.next();

                for (int x = 0; x < rs.getRow(); x++) {
                    articulo2 = articulo2 + "<tr>";
                    for (int p=2; p<5; p++) {
                        articulo2 = articulo2 + "<td>" + rs.getString(p) + "</td>";
                    }
                    for (int p=7; p<8; p++) {
                        articulo2 = articulo2 + "<td>" + rs.getString(p) + "</td>";
                    }
                    articulo2 = articulo2 + "</tr>";

                }
                conn.close();
            }
        } catch (Exception e) {
        }
        return articulo2;
    }




}
